<template>
    <view style="height: 100%">
      <scroll-view :scroll-y="true" :scroll-x="false" class="scroll" @scrolltolower="bindlower">
        <swiper class="swiper" :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval" :duration="duration" :current="current" @change="switchs">
            <swiper-item>
                <view class="listMall">
                    <block v-for="(item, index) in IndexList" :key="index">
                        <view @tap="todetail" :id="item.id" class="blocks">
                            <view class="img"><image :src="item.productPic" mode="aspectFill" /></view>
                            <view style="display:flex;">
                                <view class="name">{{ item.productTitle }}</view>
                                <text class="now_price">￥{{ item.productPrice }}</text>
                            </view>
                            <view class="desc">
                                <view>{{ item.productDesc }}</view>
                                <view v-if="item.productDiscount != null">折扣：{{ item.productDiscount }}</view>
                            </view>
                            <view v-if="item.labelNames!=null&&item.labelNames!=''">
                                <text class="name" style="margin-left:6px;">{{ item.labelNames }}</text>
                            </view>
                        </view>
                    </block>
                </view>
            </swiper-item>
        </swiper>
      </scroll-view>
      <view style="margin-top:102%">
          <button type="default" @click="share">分享</button>
          <uniPopup ref="popup" type="share">
            <uniShare title="分享到" @select="select"></uniShare>
          </uniPopup>
        </view>
    </view>
</template>

<script>
var config = require('../../../config.js');
var http = require('../../../utils/httpHelper.js');
import uniPopup from "@/components/uni-popup/uni-popup.vue";
import uniShare from "@/components/uni-popup-share-wx/uni-popup-share-wx.vue";
export default {
    components: {
			uniPopup,
			uniShare
		},
    data() {
        return {
            userInfo: {},
            isLogin: false,
            IndexList: [],
            shopId: 0,
            shop: {},
            indicatorDots: false,
            //是否显示面板指示点
            autoplay: false,
            //是否自动切换
            current: 0,
            //当前所在index
            interval: 0,
            //自动切换时间
            duration: 200,
            //滑动时间
            timer: {},
            productPage: {
                current: 1,
                pageSize: 12,
                total: 0
            }
        };
    },
    onLoad: function (options) {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        if (userInfo != null && userInfo != undefined && userInfo != {} && userInfo != '') {
            that.setData({
                isLogin: true,
                userInfo: userInfo
            });
        } else {
            uni.setStorageSync('inviteId', options.inviteId);
        }
        var shopId = options.id;
        that.getShopInfo(shopId, function (shop) {
            that.setData({
                shop: shop
            });
        });
        if (options.sell == 2) {
            uni.setNavigationBarTitle({
                title: options.shopName + '-商品展示'
            });
            that.getGoodsList(shopId, function (list) {
                that.setData({
                    shopId: shopId,
                    IndexList: list
                });
            });
        } else if (options.sell == 1) {
            uni.setNavigationBarTitle({
                title: options.shopName + '-餐品展示'
            });
            that.getMealsList(shopId, function (list) {
                that.setData({
                    shopId: shopId,
                    IndexList: list
                });
            });
        }
    },
    onShow: function () {
        let userInfo = uni.getStorageSync('user');
        if (userInfo != null && userInfo != undefined && userInfo != {} && userInfo != '') {
            this.setData({
                isLogin: true,
                userInfo: userInfo
            });
        }
    },
    /*onShareTimeline() {
        var shop = this.shop;
        var url = '/pages/shop/detail/index?id=' + shop.deptId + '&inviteId=' + this.userInfo.userId + '&shopName=' + shop.deptName + '&sell=2';
        return {
            title: shop.deptName,
            path: url,
            imageUrl: shop.shopLogo,
            desc: shop.introduce
        };
    },
    onShareAppMessage() {
        var shop = this.shop;
        var url = '/pages/shop/detail/index?id=' + shop.deptId + '&inviteId=' + this.userInfo.userId + '&shopName=' + shop.deptName + '&sell=2';
        return {
            title: shop.deptName,
            path: url,
            imageUrl: shop.shopLogo,
            desc: shop.introduce
        };
    },*/
    methods: {
        getMoreData() {
            var that = this;
            that.productPage.current++;
            that.getGoodsList(that.shopId, function (list) {
                let yList = that.IndexList;
                yList = yList.concat(list);
                that.setData({
                    IndexList: yList
                });
            });
        },

        getGoodsList: function (shopId, callback) {
            var that = this;
            var data = {
                appid: config.APPID,
                pageNo: that.productPage.current,
                pageSize: that.productPage.pageSize,
                shopId: shopId
            };
            http.httpPost(config.clubApi.getProductList, data, function (res) {
                if (res.code == '200') {
                    var list = res.data.pageData;
                    if (typeof callback == 'function') {
                        callback(list);
                    }
                }
            });
        },

        getMealsList: function (shopId, callback) {
            var that = this;
            var data = {
                appid: config.APPID,
                pageNo: that.productPage.current,
                pageSize: that.productPage.pageSize,
                shopId: shopId
            };
            http.httpPost(config.clubApi.getMealList, data, function (res) {
                if (res.code == '200') {
                    var list = res.data.pageData;
                    if (typeof callback == 'function') {
                        callback(list);
                    }
                }
            });
        },

        todetail: function (e) {
            var id = e.currentTarget.id;
            uni.navigateTo({
                url: '../../detail/index?id=' + id
            });
        },

        getShopInfo: function (deptId, callback) {
            var data = {
                deptId: deptId
            };
            http.httpGet(config.clubApi.getShopInfo, data, function (res) {
                if (res.code == '200') {
                    var shop = res.data;
                    if (typeof callback == 'function') {
                        callback(shop);
                    }
                }
            });
        },

        switchs() {
            console.log('占位：函数 switchs 未声明');
        },

        //处理分页
        bindlower: function (e) {
          if(this.timer != null){
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(()=>{
            this.getMoreData();
          },500)
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
